package com.example.myapplicationchat;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.io.File;
import java.util.List;

public class SelectedItemAdapter extends RecyclerView.Adapter<SelectedItemAdapter.ViewHolder> {

    private Context context;
    private List<String> items;

    public SelectedItemAdapter(Context context, List<String> items) {
        this.context = context;
        this.items = items;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_selected_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String item = items.get(position);
        ImageView imageView = holder.imageIv;
        if (imageView == null) return;

        if (item.startsWith("content://")) {
            Bitmap thumbnail = ThumbnailUtils.createVideoThumbnail(item, MediaStore.Video.Thumbnails.MINI_KIND);
            imageView.setImageBitmap(thumbnail);
        } else if (item.startsWith("file://")) {
            File file = new File(item.substring(7));
            Picasso.get().load(file).resize(200, 200).into(imageView);
        }
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView imageIv;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageIv = itemView.findViewById(R.id.item_image_view);
        }
    }

}